package core.actor
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;

	public class ActorCacheManager
	{
		
		private var _actorBG:Array = [new BitmapData(300,300,true,0x00000000),new BitmapData(200,200,true,0x00000000)];
		
		public function ActorCacheManager()
		{
			_cache = [];
			_needUpdateCache = [];
		}
		
		public static function get instance():ActorCacheManager
		{
			if(_instance == null)
				_instance = new ActorCacheManager();
			return _instance;
		}
		
		
		/*public function setActionBMDCache(data:Vector.<uint>,clipRect:Rectangle,part:String,actionName:String,frame:int):void
		{
			if(_cache[part] == null)
				_cache[part] = [];
			if(_cache[part][actionName] == null)
				_cache[part][actionName] = [];
			_cache[part][actionName][frame] = [data,clipRect];
		}
		
		//part = chrType+partName
		public function getActionBMDCache(part:String,actionName:String,frame:int):Array
		{
			if(_cache[part] != null
			&& _cache[part][actionName]!=null
			&& _cache[part][actionName][frame]!=null)
			{
				return _cache[part][actionName][frame];
			}
			else
				return null;
		}*/
		
		public function getActorTemplate(type:int):BitmapData
		{
			if(type<=_actorBG.length-1)
			{
				return _actorBG[type]
			}
			return null;
		}
	
		//chrTypeAndPart
		public function setActionBMDCache(data:BitmapData,clipRect:Rectangle,chrTypeAndPart:String,actionName:String,frame:int):void
		{
			if(_cache[chrTypeAndPart] == null)
				_cache[chrTypeAndPart] = [];
			if(_cache[chrTypeAndPart][actionName] == null)
				_cache[chrTypeAndPart][actionName] = [];
			_cache[chrTypeAndPart][actionName][frame] = [data,clipRect];
		}
		
		//part = chrType+partName
		public function getActionBMDCache(chrTypeAndPart:String,actionName:String,frame:int):Array
		{
			if(_cache[chrTypeAndPart] != null
				&& _cache[chrTypeAndPart][actionName]!=null
				&& _cache[chrTypeAndPart][actionName][frame]!=null)
			{
				return _cache[chrTypeAndPart][actionName][frame];
			}
			else
				return null;
		}
		
		
		
		/**
		 * @param clearAll clear all cache of the specified chrType
		 * */
		public function clearActionBMDCache(chrType:String,actionName:String = "stand",clearAll:Boolean = false):void
		{
			if(_cache[chrType] != null)
			{
				if(clearAll)
				{
					for each(var a:Array in _cache[chrType])
					{
						for each(var b:Array in a)
						{
							if(b[0]!=null && b[0] is BitmapData)
							{
								(b[0] as BitmapData).dispose();
							}
						}
					}
					_cache[chrType] = null;
					delete _cache[chrType];
				}
				else
				{
					if(_cache[chrType][actionName]!=null)
					{
						for each(var c:Array in _cache[chrType][actionName])
						{
							if(b[0]!=null && b[0] is BitmapData)
							{
								(b[0] as BitmapData).dispose();
							}
						}
						
						_cache[chrType][actionName] = null;
						delete _cache[chrType][actionName];
					}
				}
			}
				
		}
		
		private var _cache:Array;
		private var _needUpdateCache:Array;
		private static var _instance:ActorCacheManager;
	}
}